<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/01/10
 * Time: 09:31 PM
 */

namespace Home\Controller;


use Think\Controller;

class AdminController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        if (is_null($_SESSION["ADMIN"])) {
            echo '<script> alert("请先登录")</script>';
            $this->redirect("/Login/login");
        }
    }
    public function index(){
        $this->display('Admin/index');
    }

    public function userlist(){

        $count=M("userinfo")->where('type != 2')->count();
        $page=new \Think\Page($count,10);
        $list=M("userinfo")->where('type != 2')->limit($page->firstRow.','.$page->listRows)->select();
        $show=$page->show();
        $this->assign("userlist", $list);
        $this->assign("show", $show);
        $this->display('Admin/userlist');
    }

    public function deluser($id){
        $data = array('username'=>'deluser','type'=>'2');
        M('userinfo')->where('id="'.$id.'"')->setField($data);
        $this->redirect('Admin/userlist');
    }


    public function typelist(){
        $count=M("type")->count();
        $page=new \Think\Page($count,10);
        $list=M("type")->limit($page->firstRow.','.$page->listRows)->select();
        $show=$page->show();
        $this->assign("typelist", $list);
        $this->assign("show", $show);
        $this->display('Admin/typelist');
    }

    /**
     * 显示添加类型页面
     */
    public function typeadd()
    {
        $this->display("Admin/type-add");
    }

    /**
     * 添加类型逻辑
     */
    public function typeadded()
    {
        $data['name']=$_POST["typeName"];
        $type=M("type");
        $type->add($data);
        $this->assign("addtype",1);
        $this->display("Admin/type-add");
    }


    public function edittype()
    {
        $id = $_GET['id'];
        $type=M("type")->where('id='.$id)->find();
        $this->assign("type",$type);
        $this->display("Admin/type-edit");
    }

    function edittypeed(){
        $data['id'] = $_POST['id'];
        $data['name']=$_POST["typeName"];
        M('type')->save($data);

        $this->assign('addtype',1);
        $this->display("Admin/type-edit");
    }


    function deltype($type){
        M('itemlist')->where('type = "'.$type.'"')->setField('type','-1');
        M('type')->delete($type);
        $type->redirect('Admin/typelist');
    }


    public function editlist()
    {
        $id = $_GET['id'];
        $item=M("itemlist")->where('id='.$id)->find();

        $authors = M('userinfo')->select();

        $types = M('type')->select();
        $this->assign("item",$item);
        $this->assign("types",$types);
        $this->assign("authors",$authors);
        $this->display("Admin/item-edit");
    }

    function Itemlist(){
        $count=M("itemlist")->count();
        $page=new \Think\Page($count,10);
        $show=$page->show();

        $this->assign("show", $show);

        $itemlist = M('itemlist')->where('type != -1')->limit($page->firstRow.','.$page->listRows)->select();

        foreach ($itemlist as &$item)
        {
            $item['nickname']=$this->getUserNameById($item['author']);
        }

        $this->assign('itemlist',$itemlist);
        $this->display('Admin/itemlist');
    }

    function edititemed(){
        $id = $_POST['id'];
        $title = $_POST['title'];
        $author = $_POST['author'];
        $type = $_POST['type'];
//        echo $id;
//        echo $title;
//        echo $author;
//        echo $type;

//        if(M('itemlist')->where("id=$id")->setField(array('title',$title),array('author',$author),array('type',$type))){
//            echo 'fff';
//        }else{
//         dump(  M('itemlist')->getLastSql());
//        }

        $data = M('itemlist')->where("id=$id")->find();
        $data['title'] = $title;
        $data['author'] = $author;
        $data['type'] = $type;
        M('itemlist')->save($data);
        $this->assign('addtype',1);
        $this->display("Admin/item-edit");
    }

    function delitem($id){
        M('itemlist')->where("id=$id")->delete();
        $this->redirect("/Admin/Itemlist");
    }


    function delitemf($id){
        M('itemlist')->where("id=$id")->setField('type','-1');;
        $this->redirect("/Admin/Itemlist");
    }

    public function getUserNameById($id){
        $user = M('userinfo')->where('id = "'.$id.'"')->find();
        if ($user) {
            return $user['nickname'];
        }else{
            return "游客";
        }
    }

    public function recyclelist(){
        $count=M("itemlist")->where('type = -1')->count();
        $page=new \Think\Page($count,10);
        $show=$page->show();

        $this->assign("show", $show);

        $itemlist = M('itemlist')->where('type = -1')->limit($page->firstRow.','.$page->listRows)->select();

        foreach ($itemlist as &$item)
        {
            $item['nickname']=$this->getUserNameById($item['author']);
        }
        $this->assign('itemlist',$itemlist);
        $this->assign('isrecycle',true);
        $this->display('Admin/itemlist');
    }


    public function useredit($id)
    {
        $user= M('userinfo')->where('id='.$id)->find();
        $this->assign('user',$user);
        $this->display('Admin/user-edit');
    }


    public function saveuser()
    {
        $id=$_POST['id'];
        $data['nickname']=$_POST['nickname'];
        $data['type']=$_POST['type'];

        M('userinfo')->where('id='.$id)->setField($data);
        $this->assign('addtype',1);
        $this->display('user-edit');

    }
    public function userpwd(){
        $this->assign('id',1);
        $this->display('user-pwd');
    }
    public function userpwd_save()
    {
        $id=$_POST['id'];
        $pwd = $_POST['pwd'];
        $data['password']=md5($pwd);
        M('userinfo')->where('id='.$id)->setField($data);
        $this->assign('addtype',1);
        $this->display('user-pwd');
    }


}